package com.yanqu.road.logic.activity.hotpotjump.game.choose;

import com.yanqu.road.logic.activity.hotpotjump.game.JumpBoardGrid;
import com.yanqu.road.logic.activity.hotpotjump.game.eJumpDirection;

import java.util.*;

/**
 * 3 多边形围起来的所有点
 */
public class HotpotJumpSkillChoose3 extends HotpotJumpSkillChoose {
    @Override
    public List<List<JumpBoardGrid>> chooseGrid() {
        List<List<JumpBoardGrid>> result = new ArrayList<>();
        if (gameLogic.getPolygonGridSet().isEmpty()) {
            return result;
        }

        List<JumpBoardGrid> allPolygonGrids = new ArrayList<>();//组成多边形的所有点

        List<JumpBoardGrid> allMoveGrids = gameLogic.getAllMoveGrids();

        Set<JumpBoardGrid> endCloseGrids = gameLogic.board.findEndCloseGrids(new HashSet<>(allMoveGrids));//被包围起来的点

        JumpBoardGrid firstPolygonGrid = null;
        for (int i = 0; i < gameLogic.getLastPolygonIdx(); i++) {
            JumpBoardGrid moveGrid = allMoveGrids.get(i);
            if (firstPolygonGrid == null) {
                if (gameLogic.getPolygonGridSet().contains(moveGrid)) {
                    firstPolygonGrid = moveGrid;
                    allPolygonGrids.add(moveGrid);
                }
                continue;
            }
            boolean isNeedAdd = true;
            if (gameLogic.getPolygonGridSet().contains(moveGrid)) {
                JumpBoardGrid beforeGrid = null;
                if (i > 0) {
                    beforeGrid = allMoveGrids.get(i - 1);
                }
                JumpBoardGrid afterGrid = null;
                if (i < allMoveGrids.size() - 1) {
                    afterGrid = allMoveGrids.get(i + 1);
                }
                if (beforeGrid != null && afterGrid != null) {
                    if (gameLogic.getPolygonGridSet().contains(beforeGrid) && gameLogic.getPolygonGridSet().contains(afterGrid)) {
                        if (beforeGrid.calcTargetDirection(afterGrid) != eJumpDirection.None) {//在一个直线
                            //路线前后的点是交叉过的点,并且在同一个直线上,这种点可以暂时排除掉
                            //再判断一下不能和被包起来的点相邻
                            boolean isEndCloseNeighbor = false;
                            List<JumpBoardGrid> moveGridNeighbors = gameLogic.board.getNeighbors(moveGrid);
                            for (JumpBoardGrid moveGridNeighbor : moveGridNeighbors) {
                                if (endCloseGrids.contains(moveGridNeighbor)) {
                                    isEndCloseNeighbor = true;
                                    break;
                                }
                            }
                            isNeedAdd = isEndCloseNeighbor;
                        }
                    }
                }
            }

            if (isNeedAdd) {
                allPolygonGrids.add(moveGrid);
            }
        }


        if (!endCloseGrids.isEmpty()) {
            allPolygonGrids.addAll(endCloseGrids);
        }
        result.add(allPolygonGrids);
        return result;
    }
}
